package cn.fl.kgs.utils;

import cn.fl.kgs.constant.KgsConstant;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;

/**
 * 用户鉴权验证工具类
 */
public class AuthenticationUtils {
    private static final Logger logger = LoggerFactory.getLogger(AuthenticationUtils.class);
    /**
     * 根据
     * @return
     */
    public static String reqAuthenticationByappId(){
        JSONObject params = new JSONObject();
        params.put("clientName", KgsConstant.KGS_CLIENT_NAME);
        params.put("clientId",KgsConstant.KGS_CLIENT_ID);
        params.put("clientSecret",KgsConstant.KGS_CLIENT_SECRET);
        // 请求体
        String body =params.toJSONString();
        //获取当前时间戳
        Long timestamp = System.currentTimeMillis();
        String data = "";
        String token = "";
        try{
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost httpPost= new HttpPost(KgsConstant.KGS_CLIENT_GET_TOKEN);
            httpPost.setEntity(new StringEntity(body, "UTF-8"));
            httpPost.setHeader("Content-Type","application/json");
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if(entity!=null){
                data = EntityUtils.toString(entity,"utf-8");
                JSONObject jo = (JSONObject) JSONObject.parse(data);
                token = jo.get("data").toString();
                logger.info("获取token返回数据，data"+ data);
            }else{
                logger.info("获取token返回数据为空，data"+ data);
            }
        }catch (Exception e){
            logger.info("建立连接connect.异常"+ e);
        }
        return token;
    }

}
